package com.example.myretrofit2.unity;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Response;

public class ResponseInterceptor implements Interceptor {
    Context context;

    public ResponseInterceptor(Context context) {
        this.context = context;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response = chain.proceed(chain.request());
        String setCookie = response.header("Set-Cookie");
//        System.out.println("1"+setCookie);
        if(!TextUtils.isEmpty(setCookie)){
            String JSESSIONID = setCookie.split(";")[0];
            SharedPreferences preferences = context.getSharedPreferences("Cookie", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("JSESSIONID",JSESSIONID);
            editor.commit();
        }

        return response;
    }
}
